package org.yaukie;

import java.util.concurrent.ConcurrentHashMap;
import org.yaukie.core.api.MyJobInter;
import org.yaukie.helper.ConfigHelper;
import org.yaukie.support.api.ClassSupportApi;
import org.yaukie.util.ObjectUtil;
import org.yaukie.util.StringUtil;

public class ClassCacheFactory {
    public static ConcurrentHashMap<String, Object> cacheMap = new ConcurrentHashMap();
    public static final String CLASS_SUPPORT_CACHE_KEY = "x1_class_support_api_key";
    public static final String CLASS_JOB_INSTANCE_CACHE_KEY = "x1_class_job_instance_key";
    public static final String defaultClassSupportApi = "org.yaukie.support.ClassSupportImpl";
    public static final String defaultClassJobInstance = "org.yaukie.core.impl.MyJobInstance";

    public ClassCacheFactory() {
    }

    public static MyJobInter getMyJobInstance() {
        return (MyJobInter)getInstance("x1_class_job_instance_key", "org.yaukie.core.impl.MyJobInstance");
    }

    public static ClassSupportApi getClassSupportApi() {
        return (ClassSupportApi)getInstance("x1_class_support_api_key", "org.yaukie.support.ClassSupportImpl");
    }

    private static Object getInstance(String cacheKey, String defaultClassName) {
        Object resultObj = null;
        resultObj = cacheMap.get(cacheKey);
        if (resultObj == null) {
            String classImpl = ConfigHelper.getString(cacheKey);
            if (StringUtil.isEmpty(classImpl)) {
                classImpl = defaultClassName;
            }

            resultObj = ObjectUtil.newInstance(classImpl);
            if (resultObj != null) {
                cacheMap.put(cacheKey, resultObj);
            }
        }

        return resultObj;
    }
}
